package net.expectx.micro.shop.controller.content;

import io.swagger.annotations.ApiOperation;
import net.expectx.common.single.util.Md5Util;
import net.expectx.micro.shop.common.AdminConstants;
import net.expectx.micro.shop.common.util.ExpectXUtil;
import net.expectx.micro.shop.dao.bean.*;
import net.expectx.micro.shop.rpc.api.ContentAdvertisingActivitiesService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author hxs
 */
@Controller
@RequestMapping(value = "/manager/content/advertising")
public class ContentAdvertisingController {
    @Autowired
    ContentAdvertisingActivitiesService contentAdvertisingActivitiesService;

    private final  static String ADVERTISING_TYPE="1";

    @ApiOperation(value = "广告管理主页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index( HttpServletRequest request) {
        return "content/advertising/index";
    }

    @ApiOperation(value = "新增广告")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "content/advertising/modify";
    }

    @ApiOperation(value = "修改广告")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        ContentAdvertisingActivities contentAdvertisings = contentAdvertisingActivitiesService.selectByPrimaryKey(id);
        modelMap.put("contentAdvertisings", contentAdvertisings);
        return "content/advertising/modify";
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            HttpServletRequest request,String name) {
        Map<String, Object> result = new HashMap<>(16);
        ContentAdvertisingActivitiesExample contentAdvertisingActivitiesExample = new ContentAdvertisingActivitiesExample();

        /**
         * 创造条件
         */
        ContentAdvertisingActivitiesExample.Criteria criteria = contentAdvertisingActivitiesExample.createCriteria();

        criteria.andIsDeletedEqualTo(AdminConstants.NO);
        criteria.andTypeEqualTo(ADVERTISING_TYPE);
        if (StringUtils.isNotBlank(name)){
            criteria.andNameEqualTo(name);
        }
        /**
         * 分页
         */
        contentAdvertisingActivitiesExample.setPageIndex(page);
        contentAdvertisingActivitiesExample.setPageSize(limit);
        /**
         * 排序
         */
        contentAdvertisingActivitiesExample.setOrderByClause("gmt_modified desc");

        List<ContentAdvertisingActivities> contentAdvertisingActivities = contentAdvertisingActivitiesService.selectByExample(contentAdvertisingActivitiesExample);

        long total = contentAdvertisingActivitiesService.countByExample(contentAdvertisingActivitiesExample);

        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", contentAdvertisingActivities);
        return result;
    }

    @ApiOperation(value = "新增会员")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(ContentAdvertisingActivities contentAdvertisingActivities) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        PermissionUser permissionUser =(PermissionUser) session.getAttribute("permissionUser");
        contentAdvertisingActivities.setGmtCreate(System.currentTimeMillis());
        contentAdvertisingActivities.setGmtModified(System.currentTimeMillis());
        contentAdvertisingActivities.setIsDeleted((byte) 0);
        contentAdvertisingActivities.setType(ADVERTISING_TYPE);
        contentAdvertisingActivities.setUserId(permissionUser.getId());
        return contentAdvertisingActivitiesService.insertSelective(contentAdvertisingActivities);
    }

    @ApiOperation(value = "修改广告")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@PathVariable("id") Long id, ContentAdvertisingActivities contentAdvertisingActivities) {
        contentAdvertisingActivities.setGmtModified(System.currentTimeMillis());
        return  contentAdvertisingActivitiesService.updateByPrimaryKeySelective(contentAdvertisingActivities);
    }

    @ApiOperation(value = "删除广告")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object physicalRemove(@PathVariable("ids") String ids) {
        ContentAdvertisingActivitiesExample contentAdvertisingActivitiesExample = new ContentAdvertisingActivitiesExample();
        contentAdvertisingActivitiesExample.createCriteria().andIdIn(ExpectXUtil.toList(ids,","));
        return contentAdvertisingActivitiesService.physicalRemoveByExample(AdminConstants.YES, contentAdvertisingActivitiesExample);
    }
}
